<template>
    <div class="common-layout">
        <div class="login">
            <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
        >
            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name" />
            </el-form-item>  
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" type="password" />
            </el-form-item>  
            
            <el-form-item style="margin-left: 150px;">
                <el-button type="primary" @click="submitForm(ruleFormRef)" >登录</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
            
            </el-form>
        </div>
    </div>
</template>

<style scoped>
.common-layout{
    background: url(E:\web前端\项目\mygoods\mygoodsweb\src\assets\bj.jpg)
    no-repeat center;
    height: 100%;
    width: 100%;
    background-size: cover;
    display: flex;
    position: absolute;
    left: 0;
    top: 0;
}
.login{
    width: 400px;
    height: 200px;
    margin: auto;
    text-align: center;
}
</style>

<script>
import { reactive } from 'vue';
import { ElForm } from 'element-plus';
import router from '@/router';
import axios from 'axios';

 
export default {
  components: {
    ElForm,
    // 其他需要的 Element Plus 组件
  },
  data() {
    return {
      formSize: 'default',
      ruleForm: reactive({
        
      }),
      locationOptions: ['Home', 'Company', 'School'],
      options: Array.from({ length: 10000 }).map((_, idx) => ({
        value: `${idx + 1}`,
        label: `${idx + 1}`,
      })),
      rules: {
        name: [
          { required: true, message: '用户名呢？', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1-10', trigger: 'blur' },
        ],password: [
          { required: true, message: '密码呢？', trigger: 'blur' },
          { min: 3, max: 10, message: '长度3-10', trigger: 'blur' },
        ],       
      },
      ruleFormRef: null,
      userlist:[]
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.ruleFormRef = this.$refs.ruleFormRef;
    });
    
    axios.get('user.json').then(res =>{
        // console.log(res.date.userlist);
        this.userlist=res.data.userslist
    })
  },
  methods: {
    async submitForm() {
      if (!this.ruleFormRef) return;
      await this.ruleFormRef.validate((valid, fields) => {
        if (valid) {
            //此处完成用户名密码匹配，成功跳转至首页
            //使用some函数对数组进行遍历，查找符合条件的数据，找到返回TRUE，失败返回false
            let flag = this.userlist.some(v => v.name == this.ruleForm.name && v.password == this.ruleForm.password)
            if(!flag){
                alert('这都忘了，不行啊老弟')
            }else{
                //跳转首页
                sessionStorage.setItem('username',this.ruleForm.name)
                router.push('/')
            }

          console.log('submit!');
        } else {
          console.log('error submit!', fields);
        }
      });
    },
    resetForm() {
      if (!this.ruleFormRef) return;
      this.ruleFormRef.resetFields();
    },
  },
  
};
</script>